<template>
  <el-table :data="tableData" style="width: 100%" row-key="id">
    <el-table-column prop="title" label="权限名称" />
    <el-table-column label="图标" >
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon size="20px">
            <component :is="mapIcons[scope.row.icon]"></component>
          </el-icon>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作" >
      <template #default="scope">
        <el-button type="primary" round @click="handleUpdate(scope.row)">更改</el-button>
        <el-popconfirm
            title="确认删除此权限?"
            confirm-button-text="确认"
            cancel-button-text="取消"
            @confirm="handledelete(scope.row)"
        >
          <template #reference>
            <el-button type="danger" round>删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
<!--对话框-->
  <el-dialog v-model="dialogVisible" title="更新权限内容"  width="30%">
    <el-form
        ref="updateFormRef"
        :model="updateForm"
        :rules="rules"
        label-width="80px"
        class="demo-ruleForm"
        status-icon
    >
      <el-form-item label="权限名:" prop="title">
        <el-input v-model="updateForm.title"/>
      </el-form-item>
<!--      <el-form-item label="图标名:" prop="icon">-->
<!--        <el-input v-model="updateForm.icon"/>-->
<!--      </el-form-item>-->
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm(updateFormRef)">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import axios from "axios";
import {HomeFilled, Key, List, OfficeBuilding, UploadFilled, User} from "@element-plus/icons-vue";
import {onMounted} from "vue";

//渲染表格
const tableData = ref([])
//更新权限内容提示框
const currentItem=ref({})
const dialogVisible=ref(false)
const updateFormRef=ref()
const updateForm=reactive({
  title: "",
  // icon: ""
})
const rules=reactive({
  title: [
    { required: true, message: '请输入权限名', trigger: 'blur' },
  ],
  // icon: [
  //   { required: false, message: '请输入图表名', trigger: 'blur' },
  // ],
})
//获取用户权限
onMounted(()=>{
  getList()
})
const getList=async ()=>{
  var res=await axios.get("/adminapi/rights")
  tableData.value=res.data
}
//图标映射
const mapIcons={
  List,
  UploadFilled,
  OfficeBuilding,
  Key,
  User,
  HomeFilled,
}
//更新权限内容
const handleUpdate=(item)=>{
  currentItem.value=item
  updateForm.title=item.title
  // updateForm.icon=item.icon
  dialogVisible.value=true
}
//提示框确认提交
const handleConfirm=(formEl)=>{
  formEl.validate(async (valid, fields) => {
    if (valid) {
      await axios.put(`/adminapi/rights/${currentItem.value.id}`,updateForm)
      dialogVisible.value=false
      await getList()
    } else {
      console.log('登录失败!', fields)
    }
  })
}
//删除权限
const handledelete=async (item)=>{
  await axios.delete(`/adminapi/rights/${item.id}`)
  await getList()
}
</script>
